<template>
  <el-drawer
    class="dia-style"
    :title="title"
    :visible.sync="isOpen"
    :close-on-click-modal="false"
    size="50%"
    append-to-body
    :wrapper-closable="false"
     @close="cancel"
  >
    <el-form
      ref="queryForm"
      :model="queryParams"
      size="small"
      :inline="true"
      @submit.native.prevent
    >
      <el-form-item label="名称" prop="keyword">
        <el-input
          v-model="queryParams.keyword"
          placeholder="请输入名称"
          clearable
          style="width: 240px"
          maxlength="20"
          @clear="handleQuery"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          :header-cell-style="{ background: '#F3F5FC' }"
          @click="handleQuery"
        >查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button

          icon="el-icon-refresh"
          size="mini"
          :header-cell-style="{ background: '#F3F5FC' }"
          @click="handleReset"
        >重置</el-button>
      </el-form-item>
    </el-form>
    <hk-table
      ref="tableRef"
      v-bind="$attrs"
      :table-data="tableList"
      :table-columns="tableColumns"
      :show-index="true"
      :total="total"
      :selection="isMultiple"
      :loading="loading"
      :dict-list="dictList"
      :table-height="80"
      :query-params="queryParams"
      @handleSelectionChange="selectionChange"
      @changePage="getDataList"
      @singleElection="singleElection"
    />

    <div class="el-drawer-footer">
      <el-button type="primary" @click="submitForm">保 存</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-drawer>
</template>
<script>
import HkTable from '@/components/Hk-Table/index.vue'
export default {
  components: {
    HkTable
  },
  props: {
    title: {
      type: String,
      default() {
        return '编辑'
      }
    },
    formData: {
      type: Object,
      default() {
        return {}
      }
    },
    checkData: {
      type: Array,
      default() {
        return []
      }
    },
    size: {
      type: Number,
      default: 10
    },
    isMultiple: {
      type: Boolean,
      default() {
        return false
      }
    },
    pageFunction: {
      type: Function,
      default: function() {}
    },
    tableColumns: {
      type: Array,
      default: () => []
    },
    transQueryParams: {
      type: Object,
      default: () => {
        return {
          current: 1,
          size: 10
        }
      }
    },
    dictList: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      loading: false,
      total: 0,
      tableList: [],
      checkNodes: [],
      queryParams: this.transQueryParams,
      isOpen: this.open,
      templateSelection: ''
    }
  },
  mounted() {},

  methods: {
    initSelect(row) {
      this.$nextTick(() => {
        row.forEach(i => {
          this.$refs.tableRef.toggleRow(i)
        })
      })
    },
    indexMethod(index) {
      if (!this.queryParams.current || !this.queryParams.size) {
        return index + 1
      } else {
        return index + (this.queryParams.current - 1) * this.queryParams.size + 1
      }
    },
    async show(data) {
      this.isOpen = true
      // this.queryParams.productName = ''
      this.queryParams.current = 1
      await this.getDataList()
      this.$nextTick(() => {
        if (this.isMultiple) {
          this.checkNodes = data
          this.initSelect(data)
        } else {
          if (data.length) {
            this.singleElection(data[0])
          } else {
            this.templateSelection = []
          }
        }
      })
    },
    reset() {
      this.$nextTick(() => {
        this.templateSelection = []
        this.resetForm('queryForm')
      })
      this.queryParams = { size: 10, current: 1 }
    },
    /** 获取产品列表*/
    getDataList(val) {
      return new Promise((resolve, reject) => {
        this.loading = true
        this.queryParams.scrollToTop = true
        this.queryParams = { ...this.queryParams, ...val }
        this.pageFunction(this.queryParams).then(res => {
          this.tableList = res.list ? res.list : res
          this.total = res.total ? res.total : 0
          this.loading = false
          resolve(this.tableList)
        })
      })
    },
    /** 提交按钮 */
    submitForm: function() {
      if (this.checkNodes.length > 0) {
        // this.$modal.msgSuccess('修改成功')
      }
      this.$emit('handleSubmit', this.checkNodes)
      this.isOpen = false
      // this.initSelect(this.checkNodes)
      // this.reset();
    },
    // 取消按钮
    cancel() {
      this.isOpen = false
      this.queryParams={
        size:10,
        current:1
      }
      this.reset()
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.loading = true
      this.getDataList()
    },
    handleReset() {
      this.queryParams = {}
      this.getDataList()
    },
    /** 勾选表格*/
    selectionChange(w, node) {
      if (this.isMultiple) {
        this.checkNodes = w
      }
    },
    /** 单选按钮事件 */
    singleElection(row) {
      this.templateSelection = row.id
      this.checkNodes = this.tableList.filter(item => item.id === row.id)
    }
    /** 选中数据*/
    // toggleSelection(rows) {
    //   if (rows) {
    //     rows.forEach((row) => {
    //       this.$refs.tableRef.toggleRow(row)
    //     })
    //   } else {
    //     this.$refs.tableRef.clearSelection()
    //   }
    // }
  }
}
</script>

    <style lang="scss" scoped>
     #app.dark-theme{
    .container,footer,.pagination-container{
      background:transparent !important;
    }
    .dia-style{
      background:rgba(59,139,255,.1)
    }
  .sp-flex i{
    color:#4b6eef !important;
  }
}
::v-deep {
  .targetTable {
    margin-bottom: 60px;
     height:calc(100% - 130px);
  }
}
::v-deep .el-table {
  overflow: hidden !important;
}
::v-deep .el-dialog__body {
  padding: 0 20px 20px !important;
}
.el-cascader {
  width: 100%;
}
::v-deep .el-radio__label {
  display: none !important;
}
</style>

